1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| //方法1 function insertDiv(num) { let frame = document.createDocumentFragment() for(let i = 0;i<100;i++){ let div = document.createElement('div') frame.appendChild(div) } document.body.appendChild(frame) if(100*num<20000000000){ console.log(num) setTimeout(function () { insertDiv(num+1) },17) } } setTimeout(function () { insertDiv(1) },17) //方法2 function insertDiv(num) { let frame = document.createDocumentFragment() for(let i = 0;i<100;i++){ let div = document.createElement('div') frame.appendChild(div) } document.body.appendChild(frame) if(100*num<20000000000){ window.requestAnimationFrame(function () { insertDiv(num+1) }) } } window.requestAnimationFrame(function () { insertDiv(1) })
|